package com.prp.bo;

import java.text.SimpleDateFormat;
import java.util.List;

import org.hibernate.Transaction;

import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.IRecommendationEntityDAO;
import com.prp.db.idao.ISubjectCategoryDAO;
import com.prp.db.idao.ISubjectChildCategoryDAO;
import com.prp.db.idao.IUserInformationDAO;
import com.prp.db.po.RecommendationEntity;
import com.prp.db.po.SubjectCategory;
import com.prp.db.po.SubjectChildCategory;
import com.prp.db.po.SubjectRecommendation;
import com.prp.gwt.client.vo.EntityList;
import com.prp.gwt.client.vo.SubjectCategoryList;
import com.prp.gwt.client.vo.SubjectChildCategoryList;
import com.prp.gwt.client.vo.SubjectRecommendList;

public class Entity {

	// public static void main(String [] args){
	// System.out.println(HibernateSessionFactory.getSession().getFlushMode());
	// System.out.println(createEntity(4, "tsddent").getId());
	// }

	public static com.prp.gwt.client.vo.Entity createEntity(
			int childCategoryId, String entityName) {
		IRecommendationEntityDAO dao = DAOFactory.getDAOFactory()
				.getRecommendationEntityDAO();
		// Transaction tx = null;
		// try {
		// tx = HibernateSessionFactory.getSession().beginTransaction();
		// tx.begin();
		if (!validateCategoryChildId(childCategoryId))
			return null;
		List<RecommendationEntity> list = dao
				.findByChildCategoryId(childCategoryId);
		for (RecommendationEntity entity : list)
			if (entity.getEntityName().equals(entityName))
				return parse(entity);
		RecommendationEntity entity = new RecommendationEntity();
		entity.setChildCategoryId(childCategoryId);
		entity.setEntityName(entityName);
		entity.setClickFreq(0);
		entity.setWaitingTime(0);
		entity.setReplyNum(0);
		dao.save(entity);
		// tx.commit();
		return parse(entity);
		// } catch (Exception e) {
		// if (tx != null)
		// tx.rollback();
		// e.printStackTrace();
		// return null;
		// } finally {
		// HibernateSessionFactory.getSession().close();
		// }
	}

	private static boolean validateCategoryChildId(int categoryChildId) {
		ISubjectChildCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectChildCategoryDAO();
		SubjectChildCategory scc = dao.findById(categoryChildId);
		if (scc == null)
			return false;
		return true;
	}

	public static boolean createChildCategory(int categoryId, String name) {
		ISubjectChildCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectChildCategoryDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			if (!validateCategoryId(categoryId))
				return false;
			List<SubjectChildCategory> list = dao.findByCategoryId(categoryId);
			for (SubjectChildCategory scc : list)
				if (scc.getChildCategoryName().equals(name))
					return false;
			SubjectChildCategory scc = new SubjectChildCategory();
			scc.setCategroyId(categoryId);
			scc.setChildCategoryName(name);
			dao.save(scc);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}

	private static boolean validateCategoryId(int categoryId) {
		ISubjectCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectCategoryDAO();
		if (dao.findById(categoryId) == null)
			return false;
		return true;
	}

	public static boolean createCategory(String name) {
		ISubjectCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectCategoryDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			if (!dao.findByCategoryName(name).isEmpty())
				return false;
			SubjectCategory sc = new SubjectCategory();
			sc.setCategoryName(name);
			sc.setClickFreq(0);
			sc.setWaitingTime(0);
			sc.setReplyNum(0);
			dao.save(sc);
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return false;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}

	public static SubjectCategoryList getSubjectCategoryList() {
		ISubjectCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectCategoryDAO();
		List<SubjectCategory> list = dao.findAll();
		SubjectCategoryList sclist = new SubjectCategoryList();
		for (SubjectCategory sc : list) {
			com.prp.gwt.client.vo.SubjectCategory vsc = new com.prp.gwt.client.vo.SubjectCategory();
			vsc.setId(sc.getCategroyId());
			vsc.setName(sc.getCategoryName());
			sclist.add(vsc);
		}
		return sclist;
	}

	public static SubjectChildCategoryList getSubjectChildCategoryList(
			int categoryId) {
		ISubjectChildCategoryDAO dao = DAOFactory.getDAOFactory()
				.getSubjectChildCategoryDAO();
		SubjectCategory parent = DAOFactory.getDAOFactory()
				.getSubjectCategoryDAO().findById(categoryId);
		if(parent == null)
			return null;
		List<SubjectChildCategory> list = dao.findByCategoryId(categoryId);
		SubjectChildCategoryList scclist = new SubjectChildCategoryList();
		scclist.setParent(parent.getCategoryName());
		for (SubjectChildCategory scc : list) {
			com.prp.gwt.client.vo.SubjectChildCategory vscc = new com.prp.gwt.client.vo.SubjectChildCategory();
			vscc.setId(scc.getChildCategoryId());
			vscc.setName(scc.getChildCategoryName());
			scclist.add(vscc);
		}
		return scclist;
	}

	public static EntityList search(String keyword) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IRecommendationEntityDAO rDao = factory.getRecommendationEntityDAO();
		List<RecommendationEntity> list = rDao.searchByPattern("%" + keyword
				+ "%");
		EntityList entityList = new EntityList();
		for (RecommendationEntity entity : list)
			entityList.add(parse(entity));
		return entityList;
	}

	public static com.prp.gwt.client.vo.Entity getEntity(int entityId) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IRecommendationEntityDAO rDao = factory.getRecommendationEntityDAO();
		RecommendationEntity entity = rDao.findById(entityId);
		if (entity == null)
			return null;
		return parse(entity);
	}

	public static EntityList getEntityList(int childCategoryId) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IRecommendationEntityDAO rDao = factory.getRecommendationEntityDAO();
		ISubjectChildCategoryDAO sccDao = factory.getSubjectChildCategoryDAO();
		List<RecommendationEntity> list = rDao
				.findByChildCategoryId(childCategoryId);
		EntityList entityList = new EntityList();
		for (RecommendationEntity entity : list)
			entityList.add(parse(entity));
		return entityList;
	}

	protected static com.prp.gwt.client.vo.Entity parse(
			RecommendationEntity entity) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectChildCategoryDAO sccDao = factory.getSubjectChildCategoryDAO();
		ISubjectCategoryDAO scDao = factory.getSubjectCategoryDAO();
		com.prp.gwt.client.vo.Entity e = new com.prp.gwt.client.vo.Entity();
		e.setId(entity.getEntityId());
		e.setName(entity.getEntityName());
		e.setChildCategoryId(entity.getChildCategoryId());
		SubjectChildCategory scc = sccDao.findById(entity.getChildCategoryId());
		e.setChildCategoryName(scc.getChildCategoryName());
		e.setCategoryId(scc.getCategroyId());
		e
				.setCategoryName(scDao.findById(scc.getCategroyId())
						.getCategoryName());
		e.setClickFreq(entity.getClickFreq());
		e.setReplyNum(entity.getReplyNum());
		if (entity.getPic() != null)
			e.setPic(entity.getPic());
		else
			e.setPic(SNSConstants.ENTITY_PIC);
		return e;
	}

	public static EntityList getTopEntity() {
		List<com.prp.db.po.RecommendationEntity> list = Recommendation
				.getRecommendationEntityRecommend(10);
		EntityList entityList = new EntityList();
		for (RecommendationEntity entity : list) {
			entityList.add(parse(entity));
		}
		return entityList;
	}

	public static EntityList getRecentEntity() {
		IRecommendationEntityDAO dao = DAOFactory.getDAOFactory()
				.getRecommendationEntityDAO();
		List<com.prp.db.po.RecommendationEntity> list = dao.findRecentEntity();
		EntityList entityList = new EntityList();
		for (RecommendationEntity entity : list) {
			entityList.add(parse(entity));
		}
		return entityList;
	}

}
